package engine.logic.loading
{
	import engine.logic.loading.effect.AbstractTransitionEffect;
	import engine.logic.loading.effect.Fade;
	import engine.logic.loading.effect.TEFactory;
	import engine.logic.loading.effect.TransitionEffectEvent;
	import engine.scene.SceneChangeManager;
	
	import newx.StageInfo;

	public class EffectLoading
	{
		public function EffectLoading()
		{
		}
		
		static private var _self:EffectLoading;
		static public function getInstance():EffectLoading
		{
			if(_self == null)
				_self = new EffectLoading();
			
			return _self;
		}
		//
		private const EffectIn:String = "EffectIn";
		private const EffectInOver:String = "EffectInOver";
		private const EffectOut:String = "EffectOut";
		private const EffectOutOver:String = "EffectOutOver";
		
		private var _isLoadOver:Boolean;
		
		private var _effectProcess:String;
		private var _transitionEffect:AbstractTransitionEffect;
		
		private var _callAfterInOverArr:Array = [];
		
		public function setProcess(proc:Number,titleStr:String=""):void
		{
			if(_effectProcess == null || _effectProcess == EffectOutOver)
			{
				_isLoadOver = false;
				if(proc != 100)
					startEffectIn();
			}
			
			if(proc>=100)
			{
				_isLoadOver = true;
				if(_effectProcess == EffectInOver)
				{
					startEffectOut();
				}
				LoadingLogic.getInstance().hide();
			}
			else
			{
				if(_effectProcess != EffectIn && _effectProcess != EffectOut)
						LoadingLogic.getInstance().setProcess(proc,titleStr);
			}
		}
		
		public function callLater(func:Function,pList:Array):void
		{
			_callAfterInOverArr.push([func,pList]);
			
			if(_effectProcess != EffectIn)
			{
				callInOver();
			}
		}
		
		/**
		 *调用进入动画播放完的延迟函数,
		 * 调用完后删除
		 * 
		 */		
		private function callInOver():void
		{
			var callArr:Array;
			while(_callAfterInOverArr.length>0)
			{
				callArr = _callAfterInOverArr.shift();
				callArr[0].apply(null,callArr[1]);
			}
		}
		
		protected function startEffectIn():void
		{
			_effectProcess = EffectIn;
			
//			_transitionEffect = TEFactory.getInstance().getTE(TEFactory.FADE);
			_transitionEffect = TEFactory.getInstance().getTE();
			
			_transitionEffect.addEventListener(TransitionEffectEvent.EffectInOver,inOverHandler);
			StageInfo.systemSpace.addChild(_transitionEffect);
			resizeEffect();
			_transitionEffect.effectIn();
		}
		
		protected function startEffectOut():void
		{
			_effectProcess = EffectOut;
			
//			_transitionEffect = TEFactory.getInstance().getTE(TEFactory.FADE);
			_transitionEffect = TEFactory.getInstance().getTE();
			
			_transitionEffect.addEventListener(TransitionEffectEvent.EffectOutOver,outOverHandler);
			StageInfo.systemSpace.addChild(_transitionEffect);
			resizeEffect();
			_transitionEffect.effectOut();
		}
		
		public function resizeEffect():void
		{
			if(_transitionEffect && _transitionEffect.stage)
			{
				_transitionEffect.x = SceneChangeManager.getGameX();
				_transitionEffect.y = SceneChangeManager.getGameY();
				_transitionEffect.width = Math.min(StageInfo.rootMaxW, SceneChangeManager.getGameWidth());
				_transitionEffect.height = Math.min(StageInfo.rootMaxH, SceneChangeManager.getGameHeight());
			}
		}
		
		private function inOverHandler(e:TransitionEffectEvent):void
		{
			AbstractTransitionEffect(e.target).removeEventListener(TransitionEffectEvent.EffectInOver,inOverHandler);
			
			callInOver();
			
			_transitionEffect.parent.removeChild(_transitionEffect);
			_effectProcess = EffectInOver;
			if(_isLoadOver)
			{
				startEffectOut();
			}
		}
		
		private function outOverHandler(e:TransitionEffectEvent):void
		{
			AbstractTransitionEffect(e.target).removeEventListener(TransitionEffectEvent.EffectInOver,outOverHandler);
			_transitionEffect.parent.removeChild(_transitionEffect);
			_effectProcess = EffectOutOver;
		}
		
	}
}